package com.school.util;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import org.springframework.web.multipart.MultipartFile;

public class PictureVideoUpload {
	
	
	  
	  public static String savePictureVideo(MultipartFile imgFile , String address){
		  // 获取web的根路径
		  //  String ProjectPath=this.getClass().getClassLoader().getResource("/").getPath() ; 
		  //得到原始文件的名字
		  String originalFilename = imgFile.getOriginalFilename();
		  if(originalFilename=="" || null ==originalFilename){
	        	return null;
	        }
		  //后缀名点之前有多少位
		  int pos = originalFilename.lastIndexOf(".");
		  //截取得到后缀名
		  String suffix = originalFilename.substring(pos);
		  
		  if("" == originalFilename.trim()) {
			  return null;
		  }
		  //随机码
		  
		  String uuid = UUID.randomUUID().toString();
		  String fullPath = address + uuid + suffix;
	      String fileName= null;
	      InputStream inputStream=null;
	       try {
	    	   inputStream = imgFile.getInputStream();
	    	   OutputStream outputStream = new FileOutputStream(new File(fullPath));
	    		int len = 0;
				byte[] buf = new byte[3 * 1024];
				while ((len = inputStream.read(buf)) != -1) {
					outputStream.write(buf, 0, len);
				}
				//fileName ="/img/"+uuid + suffix;
				fileName ="/image/class/"+uuid + suffix;
				outputStream.close();
				inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fileName;
	}
	  
	  

}
